package com.zy.test.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.test.entity.Course;
import com.zy.test.service.CourseService;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.lang.invoke.LambdaMetafactory;
import java.util.List;

/**
 * 科目表：
 * 科目ID，科目名称，备注。课程ID为主键。(Course)表控制层
 *
 * @author jhr
 * @since 2023-04-03 16:32:30
 */
@RestController
@RequestMapping("course")
public class CourseController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private CourseService courseService;

    /**
     * 分页查询所有数据
     *
     * @param course 查询实体
     * @return 所有数据
     */
    @GetMapping("/selectByPaging")
    public R selectAll(Course course) {
        Page<Course> page = new Page<>(course.getPageNum(),course.getPageSize());
        LambdaQueryWrapper<Course> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(course.getCoCode())) {
            lambdaQueryWrapper.eq(Course::getCoCode,course.getCoCode());
        }
        if(!ObjectUtils.isEmpty(course.getCoName())) {
            lambdaQueryWrapper.like(Course::getCoName,course.getCoName());
        }
        return success(this.courseService.page(page,lambdaQueryWrapper));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectById/{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.courseService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param course 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    public R insert(@RequestBody Course course) {
        course.setCreDate(DateUtil.date());
        return success(this.courseService.save(course));
    }

    /**
     * 修改数据
     *
     * @param course 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    public R update(@RequestBody Course course) {
        course.setModiDate(DateUtil.date());
        return success(this.courseService.updateById(course));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/deleteByIds")
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.courseService.removeByIds(idList));
    }
}

